package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;
@EnableSwagger2
@RestController
@RequestMapping("admin/product")
public class BaseManagerController {

    @Autowired
    private ManageService manageService;
    //控制器
    //查询所有的一级目录
    //http://api.gmall.com/admin/product/getCategory1
    @GetMapping("getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> baseCategory1List = manageService.getCategory1();
        for (BaseCategory1 baseCategory1 : baseCategory1List) {
            System.out.println("baseCategory1 = " + baseCategory1);
        }
        //将数据返回
        return Result.ok(baseCategory1List);
    }

    //查询所有的二级分类数据
    //http://api.gmall.com/admin/product/getCategory2/{category1Id}
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){
        List<BaseCategory2> baseCategory2List = manageService.getCategory2(category1Id);
        //将数据返回
        return Result.ok(baseCategory2List);
    }

    //查询所有的三级分类数据
    //http://api.gmall.com/admin/product/getCategory3/{category2Id}
    @GetMapping("getCategory3/{category2Id}")
    public  Result getCategory3(@PathVariable Long category2Id){
        List<BaseCategory3> baseCategory3List = manageService.getCategory3(category2Id);
        //将数据返回
        return Result.ok(baseCategory3List);
    }

    //返回平台属性,和平台属性值
    //获取分类id获取平台属性
    //http://api.gmall.com/admin/product/attrInfoList/{category1Id}/{category2Id}/{category3Id}
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public  Result attrInfoList(@PathVariable Long category1Id,
                                @PathVariable Long category2Id,
                                @PathVariable Long category3Id){
        List<BaseAttrInfo> attrInfoList = manageService.getAttrInfoList(category1Id, category2Id, category3Id);
        //将数据返回
        return Result.ok(attrInfoList);
    }

    //保存数据
    //添加平台属性
    //http://api.gmall.com/admin/product/saveAttrInfo
    //修改平台属性
    //http://api.gmall.com/admin/product/saveAttrInfo
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        //调用服务层方法
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    //根据平台属性Id 获取平台属性值集合
    //http://api.gmall.com/admin/product/getAttrValueList/{attrId}
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){
        //可以先通过attrId,获取平台属性baseAttrInfo,
        // 如果属性存在,则获取属性中对应的属性值getAttrValueList
        BaseAttrInfo baseAttrInfo = manageService.getBaseAttrInfo(attrId);
        return Result.ok(baseAttrInfo.getAttrValueList());

        //查询 平台属性值的集合
//        List<BaseAttrValue> baseAttrValueList = manageService.getAttrValueList(attrId);
//        return Result.ok();
    }

}
